import re

# 使用 match 方法进行匹配
# 如果 匹配 成功, 使用 group 提取数据
# 语法: re.match(pattern, str, flag)
# match 从开始位置结束
res = re.match("leslie", "leslie")
res2 = re.match("lesliex", "leslie")
print(res)  #
print(res2)  # None
print(res.group())


print("--------- 匹配任意一个字符 --------------")
# . 匹配任意一个字符
r1 = re.match(".", "leslie")
r2 = re.match("..", "leslie")
print(r1.group())  # l
print(r2.group())  # l

print("--------- 匹配[]中列举的字符 --------------")
r3 = re.match("[l][e]", "leslie")
print(r3.group())  # le

r4 = re.match("[1-5]", "5432")
print(r4.group())  # le


r5 = re.match("[0-9]", "123")
print(r5.group())  # 1

r6 = re.match("[a-zA-Z0-9]", "1a2b3c")
print(r6.group())  # 1


print("--------- 匹配数字0-9 --------------")
r7 = re.match("\d", "234")
print(r7.group())  # 2

r8 = re.match(".\d", "s234")
print(r8.group())  # s2

r8 = re.match(".\D", "sssss")
print(r8.group())  # ss

r8 = re.match("\D", "aba12")
print(r8.group())  # a

print("--------- 匹配空格和tab --------------")
r9 = re.match("\s.", " 123")
print(r9.group())  # 空格

r10 = re.match("\S", "aaa")
print(r10.group())  # a


print("--------- 匹配单词字符 \w a-z A-Z 0-9 _ 汉字 --------------")
r11 = re.match("\w", "_saw")
print(r11.group())  # _

print("---------  \W 非单词字符 --------------")
r12 = re.match("\W", "!")
print(r12.group())  # !
